﻿using ControlzEx.Theming;
using MahApps.Metro.Controls.Dialogs;
using MetroDemo.Core;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Series;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace ZPeng.CSharpPro
{
    public class AccentColorMenuData
    {
        public string? Name { get; set; }

        public Brush? BorderColorBrush { get; set; }

        public Brush? ColorBrush { get; set; }

        public AccentColorMenuData()
        {
            this.ChangeAccentCommand = new SimpleCommand<string?>(o => true, this.DoChangeTheme);
        }

        public ICommand ChangeAccentCommand { get; }

        protected virtual void DoChangeTheme(string? name)
        {
            if (name is not null)
            {
                ThemeManager.Current.ChangeThemeColorScheme(Application.Current, name);
            }
        }
    }

    public class AppThemeMenuData : AccentColorMenuData
    {
        protected override void DoChangeTheme(string? name)
        {
            if (name is not null)
            {
                ThemeManager.Current.ChangeThemeBaseColor(Application.Current, name);
            }
        }
    }

    public class ViewModel_Mainwin : ViewModelBase, IDataErrorInfo, IDisposable
    {
        public ViewModel_Mainwin(IDialogCoordinator dialogCoordinator)
        {

            this._dialogCoordinator = dialogCoordinator;

            this.ChangeSkinCommand = new SimpleCommand<object>(
                o => true,
                async x => { await this._dialogCoordinator.ShowMessageAsync(this, "Closing tab!", $"You are now closing the '{x}' tab"); }
            );

            // create metro theme color menu items for the demo
            this.AppThemes = ThemeManager.Current.Themes
                                         .GroupBy(x => x.BaseColorScheme)
                                         .Select(x => x.First())
                                         .Select(a => new AppThemeMenuData { Name = a.BaseColorScheme, BorderColorBrush = a.Resources["MahApps.Brushes.ThemeForeground"] as Brush, ColorBrush = a.Resources["MahApps.Brushes.ThemeBackground"] as Brush })
                                         .ToList();

            this.MyModel = new PlotModel { Title = "Example 1" };
            this.MyModel.Series.Add(new FunctionSeries(Math.Cos, 0, 100, 0.1, "cos(x)"));


            this.PlotModel_Bar = new PlotModel { Title = "Example 2" };
            List<DataPoint> points = new List<DataPoint>
                              {
                                  new DataPoint(0, 4),
                                  new DataPoint(10, 13),
                                  new DataPoint(20, 15),
                                  new DataPoint(30, 16),
                                  new DataPoint(40, 12),
                                  new DataPoint(50, 12)
                              };

            var pnls = new List<DataPoint>();
            var random = new Random(31);
            var dateTime = DateTime.Today.Add(TimeSpan.FromHours(9));
            int numsLength = 10;
            for (var pointIndex = 0; pointIndex < numsLength; pointIndex++)
            {
                pnls.Add(new DataPoint(
                    pointIndex,
                    90 - random.NextDouble() * 100
                    ));
            }

            var minimum = pnls.Min(x => x.Y);
            var maximum = pnls.Max(x => x.Y);

            var plotModel = this.PlotModel_Bar;
            plotModel.Title = "Linear Bar Series Animation Demo";

            var series = new LinearBarSeries
            {
                Title = "P & L",
                ItemsSource = pnls,
                DataFieldX = "P",
                DataFieldY = "L",
                FillColor = OxyColor.Parse("#454CAF50"),
                StrokeColor = OxyColor.Parse("#4CAF50"),
                StrokeThickness = 1,
                //BarWidth = 20
            };
            plotModel.Series.Add(series);

            var annotation = new LineAnnotation
            {
                Type = LineAnnotationType.Horizontal,
                Y = 0
            };
            plotModel.Annotations.Add(annotation);

            var dateTimeAxis = new LinearAxis
            {
                Position = AxisPosition.Bottom,
                //IntervalType = DateTimeIntervalType.Hours,
                Minimum = 0,
                Maximum = numsLength,
                //IntervalLength = 100
            };
            plotModel.Axes.Add(dateTimeAxis);

            var margin = (maximum - minimum) * 0.05;

            var valueAxis = new LinearAxis
            {
                Position = AxisPosition.Left,
                Minimum = minimum - margin,
                Maximum = maximum + margin,
            };
            plotModel.Axes.Add(valueAxis);
        }

        //
        // 摘要:
        //     Gets the error message for the property with the given name.
        //
        // 参数:
        //   columnName:
        //     The name of the property whose error message to get.
        //
        // 返回结果:
        //     The error message for the property. The default is an empty string ("").
        public string this[string columnName]
        {
            get
            {
                //if (columnName == nameof(this.IntegerGreater10Property) && this.IntegerGreater10Property < 10)
                //{
                //    return "Number is not greater than 10!";
                //}

                //if (columnName == nameof(this.DatePickerDate) && this.DatePickerDate == null)
                //{
                //    return "No date given!";
                //}

                //if (columnName == nameof(this.HotKey) && this.HotKey != null && this.HotKey.Key == Key.D && this.HotKey.ModifierKeys == ModifierKeys.Shift)
                //{
                //    return "SHIFT-D is not allowed";
                //}

                //if (columnName == nameof(this.TimePickerDate) && this.TimePickerDate == null)
                //{
                //    return "No time given!";
                //}

                //if (columnName == nameof(this.IsToggleSwitchVisible) && !this.IsToggleSwitchVisible)
                //{
                //    return "There is something hidden... \nActivate me to show it up.";
                //}

                return null!;
            }
        }

        //
        // 摘要:
        //     Gets an error message indicating what is wrong with this object.
        //
        // 返回结果:
        //     An error message indicating what is wrong with this object. The default is an
        //     empty string ("").
        [Description("Test-Property")]
        public string Error => string.Empty;

        public void Dispose()
        {

        }

        #region

        public ICommand ChangeSkinCommand { get; }
        public List<AppThemeMenuData> AppThemes { get; set; }

        public int SelectedIndex_Themes { get; set; }
        #endregion


        #region private 
        private readonly IDialogCoordinator _dialogCoordinator;

        #endregion


        #region oxyplot

        public PlotModel MyModel { get; private set; }
        public PlotModel PlotModel_Bar { get; private set; }
        #endregion

    }
}
